Create Project: bootspring_http_requestbody (add Spring Boot Starters from the table)
Create Package: DTO (inside main package)
– Create Class: PersonDTO.java (inside package controllers)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside package controllers)
PersonDTO.java
package com.ivoronline.springboot_dto_jsonproperty.DTO;
import com.fasterxml.jackson.annotation.JsonProperty;
public class PersonDTO {
//JSON PROPERTIES //DTO PROPERTIES
@JsonProperty("First Name") public String name; //Completely different name (and with space)
@JsonProperty("Age") public Integer age; //Uppercase A
}
MyController.java
package com.ivoronline.springboot_dto_jsonproperty.controllers;
import com.ivoronline.springboot_dto_jsonproperty.DTO.PersonDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@ResponseBody
@RequestMapping("/AddPerson")
public String addPerson(@RequestBody PersonDTO personDTO) {
//GET DATA FROM PersonDTO
String name = personDTO.name;
Integer age = personDTO.age;
//RETURN SOMETHING
return name + " is " + age + " years old";
}
}